package com.hmjk.health.views;

import com.hmjk.health.R;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;

public class PagerIndexStrip extends LinearLayout{
	public static final int RES_TYPE_DEF = 0;
	public static final int RES_TYPE_BLACK = 1;
	private int mCount = 0;
	private int mCurrent = 0;
	private int child_w = 0;
	private int child_h = 0;
	private int mResType = RES_TYPE_DEF;
	
	private final int[] DEF_INDEX_RES_ID = {R.drawable.index_se, R.drawable.index_un};
	private final int[] BLACK_INDEX_RES_ID = {R.drawable.index_se_black, R.drawable.index_un_black};
	
	public PagerIndexStrip(Context context) {
		this(context, null);
	}
	
	public PagerIndexStrip(Context context, AttributeSet attrs){
		super(context, attrs);
		setHapticFeedbackEnabled(false);
		child_w = getResources().getDimensionPixelSize(R.dimen.pager_index_width);
		child_h = getResources().getDimensionPixelSize(R.dimen.pager_index_height);
	}
	
	public void setCount(int count){
		mCount = count;
	}
	
	public void setCurrent(int index){
		mCurrent = index;
	}
	
	public void setResType(int t){
		mResType = t;
	}
	
	private int[] getIndexRes(){
		if(RES_TYPE_BLACK == mResType)
			return BLACK_INDEX_RES_ID;
		else
			return DEF_INDEX_RES_ID;
	}
	
	public void update(){
	//	removeAllViews();
		//createIndexViews();
		//requestLayout();
		int curCount = getChildCount();
		
		if((mCount > 0)&&(mCount != curCount)){
			createIndexViews();
		}else{
			for(int i=0; i<curCount;i++){
				ImageView v = (ImageView)getChildAt(i);
				int[] ress = getIndexRes();
				int res = (mCurrent == i)?ress[0]:ress[1];
				v.setImageResource(res);
				v.setScaleType(ScaleType.CENTER_INSIDE);
			}
		}
		invalidate();
	}
	
	private void createIndexViews(){
		Context c = getContext();

		for(int i=0; i<mCount;i++){
			ImageView v = new ImageView(c);
			int[] ress = getIndexRes();
			int res = (mCurrent == i)?ress[0]:ress[1];
			v.setImageResource(res);
			v.setScaleType(ScaleType.CENTER_INSIDE);
			
			addView(v);
		}
	}
	
	private void measureIndexViews(){
		
		int wSpec = MeasureSpec.makeMeasureSpec(child_w, MeasureSpec.EXACTLY);
		int hSpec = MeasureSpec.makeMeasureSpec(child_h, MeasureSpec.EXACTLY);
		
		for(int i=0; i<mCount;i++){
			ImageView v = (ImageView)getChildAt(i);
			v.measure(wSpec, hSpec);
		}
	}
	
	@Override  
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		int widthMode = MeasureSpec.getMode(widthMeasureSpec);
		int widthSize = MeasureSpec.getSize(widthMeasureSpec);	
		int heightMode = MeasureSpec.getMode(heightMeasureSpec);
		int heightSize = MeasureSpec.getSize(heightMeasureSpec);
		
		if(widthMode != MeasureSpec.EXACTLY){
			throw new IllegalStateException("must set exact width");
		}
		
		if(heightMode != MeasureSpec.EXACTLY){
			throw new IllegalStateException("must set exact height");
		}
		
		int curCount = getChildCount();
		
		if((mCount > 0)&&(mCount != curCount)){
			createIndexViews();
		}
		measureIndexViews();
	}
	
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		//super.onLayout(changed, l, t, r, b);
		int w = child_w*mCount;
		int left = (r - l - w)/2;
		
		for(int i=0; i<mCount;i++){
			ImageView v = (ImageView)getChildAt(i);
			v.layout(left, 0, left + child_w, child_h);
			left = left + child_w;
		}
	}
	
	
}
